//
//  MessageViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/7.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class CGQMessageListViewController: SYBaseTableViewController {
    
    fileprivate var vm: VGTableViewPageViewModel<NSMessageRecordsModel>!

    var typeNum: String!
    
    init(typeNum: String) {
        super.init(nibName: nil, bundle: nil)
        self.typeNum = typeNum
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    override func viewWillAppear(_ animated: Bool) {
//
//        self.tabBarController?.tabBar.isHidden = false
//        self.navigationItem.leftBarButtonItems = []
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()
        setupViewModel()
//        self.loadDataSuccessed()
        self.title = "消息"
        
        if self.typeNum == "1" {
            
            self.title = "设备离线"
        }else if self.typeNum == "2"{
            self.title = "设备预警"

            
        }
        else if self.typeNum == "3"{
            self.title = "端口开启"

            
        }else if self.typeNum == "4"{
            self.title = "端口关闭"
        }
        
    }

    
    fileprivate func setupViewModel() {
        vm = VGTableViewPageViewModel<NSMessageRecordsModel>()
        vm.tableView = tableView
        vm.emptyViewParams = ("", "", 40.0, 30.0)
        tableView.vg_pageProtocol = vm
        vm.getList(type: .xiaoxiclass(number: self.typeNum ?? "", page: self.vm.vg_pageNo, size: self.vm.size), dispose: dispose)
        vm.getDataClosure = {[weak self] page in
            if let `self` = self {
                self.vm.getList(type: .xiaoxiclass(number: self.typeNum ?? "", page: self.vm.vg_pageNo, size: self.vm.size), dispose: self.dispose)
            }
        }
        vm.loadDataClosure = {[weak self] success in
            if let `self` = self {
                if success {
                    self.loadDataSuccessed()
                }
            }
        }
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = Constant.bgViewColor
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                    if self.typeNum == "1"  {
                        let vc = CGQxiangqingViewController(model: self.vm.dataSource[indexPath.row])
                        self.sy_push(vc)
                    }
                    if self.typeNum == "3" || self.typeNum == "4"{
                        let vc = CGQduankouDetailViewController(model: self.vm.dataSource[indexPath.row],type: self.typeNum)
                        self.sy_push(vc)
                        
                    }
             
                    
                }
            })
            .disposed(by:dispose)
    }
    
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            NSMessageRecordsModelSection(header: "", items: self.vm.dataSource)//self.vm.dataSource)
        ])

        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<NSMessageRecordsModelSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                
                let cell: CGQMsgCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQMsgCell ?? CGQMsgCell(style: .default, reuseIdentifier: "Cell")
                cell.configureView(model: item,type: self.typeNum! as NSString)
                
                cell.selectionStyle = .none
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}


//自定义Section
struct NSMessageRecordsModelSection {
    var header: String
    var items: [Item]
}

extension NSMessageRecordsModelSection : AnimatableSectionModelType {
    typealias Item = NSMessageRecordsModel

    var identity: String {
        return header
    }

    init(original: NSMessageRecordsModelSection, items: [Item]) {
        self = original
        self.items = items
    }
}

extension NSMessageRecordsModel: IdentifiableType {
    var identity: String {
        return id!
    }

    typealias Identity = String
}

